/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.DynamicConfigDTO;
import com.swak.monitor.dubbo.service.OverrideService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboOverridesApi", path = "/api/monitor/:env/rules/override")
public class OverridesApi {

	@Autowired
	private OverrideService overrideService;
	// @Autowired
	// private ProviderService providerService;

	@Post
	public boolean createOverride(DynamicConfigDTO overrideDTO, String env) {
		String serviceName = overrideDTO.getService();
		String application = overrideDTO.getApplication();
		if (StringUtils.isEmpty(serviceName) && StringUtils.isEmpty(application)) {
			throw new ParamValidationException("serviceName and application are Empty!");
		}
//        if (StringUtils.isNotEmpty(application)
//            && providerService.findVersionInApplication(application).equals("2.6")) {
//            throw new VersionValidationException("dubbo 2.6 does not support application scope dynamic config");
//        }
		overrideService.saveOverride(overrideDTO);
		return true;
	}

	@Put("/:id")
	public boolean updateOverride(String id, DynamicConfigDTO overrideDTO, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		DynamicConfigDTO old = overrideService.findOverride(id);
		if (old == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		overrideService.updateOverride(overrideDTO);
		return true;
	}

	@Get
	public List<DynamicConfigDTO> searchOverride(String service, String application, String env) {
		DynamicConfigDTO override = null;
		List<DynamicConfigDTO> result = new ArrayList<>();
		if (StringUtils.isNotBlank(service)) {
			override = overrideService.findOverride(service);
		} else if (StringUtils.isNotBlank(application)) {
			override = overrideService.findOverride(application);
		} else {
			throw new ParamValidationException("Either Service or application is required.");
		}
		if (override != null) {
			result.add(override);
		}
		return result;
	}

	@Get("/:id")
	public DynamicConfigDTO detailOverride(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		DynamicConfigDTO override = overrideService.findOverride(id);
		if (override == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}

		return override;
	}

	@Delete("/:id")
	public boolean deleteOverride(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		overrideService.deleteOverride(id);
		return true;
	}

	@Put("/enable/:id")
	public boolean enableRoute(String id, String env) {

		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		overrideService.enableOverride(id);
		return true;
	}

	@Put("/disable/:id")
	public boolean disableRoute(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);

		overrideService.disableOverride(id);
		return true;
	}
}
